package com.ff.xyh.consumer.messagedeliver.controller;

import com.ff.xyh.common.servicebase.config.MyApplicationContextAware;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jca.context.SpringContextResourceAdapter;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * Create by Wzy
 * on 2018/8/6 13:24
 * 不短不长八字刚好
 */
//@ServerEndpoint("/community/{room_user}")
//@Component
public class CommunityController {
//    // 所有房间都存放在一个变量 会有安全问题 操作时需要上锁
//    private static final Map<String, CopyOnWriteArraySet<CommunityController>> rooms = new HashMap<>();
//
//    // 把所有用户存在这里面
//    private static final Map<String, CommunityController> users = new HashMap<>();
//
//    private Session session;
//
//    private String userId;
//
//    private String roomId;
//
////    private static RedisTemplate redisTemplate;
////    private static RabbitTemplate rabbitTemplate;
////
////
////    public CommunityController() {
////        if (redisTemplate == null) {
////            if (MyApplicationContextAware.getApplicationContext() != null) {
////                redisTemplate = MyApplicationContextAware.getBean("redisTemplate", RedisTemplate.class);
////            }
////        }
////        if (rabbitTemplate == null) {
////            if (MyApplicationContextAware.getApplicationContext() != null) {
////                rabbitTemplate = MyApplicationContextAware.getBean("rabbitTemplate", RabbitTemplate.class);
////            }
////        }
////    }
//
//
//    /**
//     * 一个用户连接时生成一个controller 一个controller也会包含一个session
//     *
//     */
//    @OnOpen
//    public void onOpen(@PathParam(value = "room_user") String room_user, Session session) {
////        System.out.println(rabbitTemplate != null);
//        Map<String, List<String>> requestParameterMap = session.getRequestParameterMap();
//        System.out.println(requestParameterMap.get("ddd"));
//        System.out.println(session.getPathParameters().get("user"));
//        System.out.println(room_user);
//        this.session = session;
//        String[] param = room_user.split("-");
//        this.roomId = param[0];
//        this.userId = param[1];
//        // 一个controller相当于一个连接用户  CopyOnWriteArraySet解决线程并发安全问题
//        CopyOnWriteArraySet<CommunityController> friends = rooms.get(roomId);
//        if (friends == null) {
//            synchronized (rooms) {
//                if (!rooms.containsKey(roomId)) {
//                    friends = new CopyOnWriteArraySet<>();
//                    rooms.put(roomId, friends);
//                }
//            }
//        }
//        friends.add(this);
//    }
//
//    @OnClose
//    public void onClose() {
//        CopyOnWriteArraySet<CommunityController> friends = rooms.get(roomId);
//        if (friends != null) {
//            friends.remove(this);
//        }
//    }
//
//    /**
//     * 服务端接收消息
//     * @param message
//     * @param session
//     */
//    // 客户端向服务端
//    @OnMessage
//    public void onMessage(final String message, Session session) {
//        //新建线程来保存用户聊天信息
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
////                client.save(new UserMessage(0, userId, message, roomId, new Date()));
//            }
//        }).start();
//        System.out.println(message);
//
//        CopyOnWriteArraySet<CommunityController> friends = rooms.get(roomId);
//        if (friends != null) {
//            for (CommunityController item : friends) {
//                // 修复发送消息后自己收到了重复的消息
//                if (! item.session.equals(session)){
//                    item.session.getAsyncRemote().sendText(message);
//                    System.out.println("message被发送了");
//                }
//            }
//        }
//    }
//
//    @OnError
//    public void onError(Session session, Throwable error) {
//        System.out.println("出错啦");
//        error.printStackTrace();
//    }
//
////    public String[] generateRandomStrings(Integer count, Integer strLength){
////        String[] resultStrings = null;
////        if (count == 0)
////            return resultStrings;
////        resultStrings = new String[count];
////        Random random = new Random();
////        for (Integer integer = 0; integer < strLength; integer++) {
////            random.nextInt(3);
////            random.ints(65,87);
////        }
////        return resultStrings;
////    }
}